# Maintainer: Martell Malone <MartellMalone@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

# Martell's notes
# libc++ and libc++abi are built statically only becuase shared will depend
# on libstdc++ atm which would make a big mess for us
# We could either use libc++ for this or use a static libstdc++
# I would like to avoid the latter.

# I may add a bootstrap option for libc++ to build libc++ soon
# Maybe having a seperate PKGBUILD for it would be helpful for this also

_realname=clang
pkgbase=mingw-w64-${_realname}-svn
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-svn"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-analyzer-svn"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-tools-extra-svn"
         "${MINGW_PACKAGE_PREFIX}-compiler-rt-svn"
         "${MINGW_PACKAGE_PREFIX}-libc++abi-svn"
         "${MINGW_PACKAGE_PREFIX}-libc++-svn"
         "${MINGW_PACKAGE_PREFIX}-lld-svn"
         "${MINGW_PACKAGE_PREFIX}-llvm-svn"
         #"${MINGW_PACKAGE_PREFIX}-lldb-svn"
        )
pkgver=60430.db7c3ac
pkgrel=1
pkgdesc="C language family frontend for LLVM (svn) (mingw-w64)"
arch=('any')
url="http://llvm.org"
license=("custom:University of Illinois/NCSA Open Source License")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python3-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "git")
depends=("${MINGW_PACKAGE_PREFIX}-gcc")
options=('debug' '!strip')
source=("llvm"::"git+http://llvm.org/git/llvm.git"
        "lld"::"git+http://llvm.org/git/lld.git"
        "clang"::"git+http://llvm.org/git/clang.git"
        "clang-tools-extra"::"git+http://llvm.org/git/clang-tools-extra.git"
        "compiler-rt"::"git+http://llvm.org/git/compiler-rt.git"
        "testsuite"::"git+http://llvm.org/git/test-suite.git"
        "libcxxabi"::"git+http://llvm.org/git/libcxxabi.git"
        "libcxx"::"git+http://llvm.org/git/libcxx.git"
        "lldb"::"git+http://llvm.org/git/lldb.git"
        0001-Fix-GetHostTriple-for-mingw-w64-in-msys.patch
        0002-use-DESTDIR-on-windows.patch
        0003-generate-proper-library-names-mingw.patch
        0014-dont-create-cl-mingw.patch
        0018-mingw-enable-static-libclang.patch
        0041-libcxx-add-support-for-mingw-w64.patch)

# Some patch notes :)
#0001-0009 -> llvm
#0011-0019 -> clang
#0021-0029 -> rt
#0031-0039 -> lld
#0041-0049 -> c++
#0051-0059 -> lldb

sha256sums=('SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            'SKIP'
            '0804146b32138d55c611336cc81e1783c29a8fab0fe62f248ba1ad7acc711c4d'
            '2671d9644b135dc81daa9d28290799ef51d3e14ebfc5791eb2102e8ede894b89'
            'c50bb58575072cf05986adfff737c27477b145cb4e9e2edb625ec34efe9ad23f'
            'b03cfc7ebbbfb847e88ae3569d9dcafb01f179b06f1312de29fbd5b7cf906617'
            'd929ef7c4c18c65f7a48156ff3ca38453334822008f2a5f57dd3f1d7c61b6723'
            '5b8edbbb638c906216e20229529e8348abc50d5886d20e07af08543e1e574e94')

pkgver_internal() {
  if [ ! -d "$1" ]; then
    plain "No such directory $1"
    exit 1
  fi
  pushd "$1" > /dev/null 2>&1
  printf "%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
  popd > /dev/null 2>&1
}

pkgver() {
  pkgver_internal "${srcdir}"/clang
}

prepare() {
  cd "${srcdir}"/llvm
  git am "${srcdir}"/0001-Fix-GetHostTriple-for-mingw-w64-in-msys.patch
  git am "${srcdir}"/0002-use-DESTDIR-on-windows.patch
  git am "${srcdir}"/0003-generate-proper-library-names-mingw.patch

  # Fix docs installation directory
  sed -i 's:$(PROJ_prefix)/docs/llvm:$(PROJ_prefix)/share/doc/llvm:' \
    Makefile.config.in

  # Fix definition of LLVM_CMAKE_DIR in LLVMConfig.cmake
  sed -i '/@LLVM_CONFIG_CMAKE_DIR@/s:$(PROJ_cmake):$(PROJ_prefix)/share/llvm/cmake:' \
    cmake/modules/Makefile

  cd "${srcdir}"/compiler-rt

  cd "${srcdir}"/lld

  cd "${srcdir}"/libcxx
  git am "${srcdir}"/0041-libcxx-add-support-for-mingw-w64.patch

  cd "${srcdir}"/clang
  git am "${srcdir}"/0014-dont-create-cl-mingw.patch
  git am "${srcdir}"/0018-mingw-enable-static-libclang.patch

  cd "${srcdir}"/llvm

  # At the present, clang must reside inside the LLVM source code tree to build
  # See http://llvm.org/bugs/show_bug.cgi?id=4840
  mv "${srcdir}/clang"             tools/clang
  mv "${srcdir}/clang-tools-extra" tools/clang/tools/extra
  mv "${srcdir}/lld"               tools/lld
  mv "${srcdir}/lldb"              tools/lldb
  mv "${srcdir}/compiler-rt"       projects/compiler-rt
  mv "${srcdir}/libcxxabi"         projects/libcxxabi
  mv "${srcdir}/libcxx"            projects/libcxx
# mv "${srcdir}/testsuite"         projects/test-suite
}

build() {
  cd "${srcdir}"

  [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  mkdir build-${CARCH} && cd build-${CARCH}

  local -a extra_config

  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
    VERBOSE="VERBOSE=1"
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  # Include location of libffi headers in CPPFLAGS
  FFI_INCLUDE_DIR="$(pkg-config --cflags libffi)"
  FFI_INCLUDE_DIR=$(echo $FFI_INCLUDE_DIR | sed 's|-I||g')

  # "Ninja" cant install each component seperately
  # https://github.com/martine/ninja/issues/932

  PATH=${MINGW_PREFIX}/bin:/usr/bin \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DCMAKE_MAKE_PROGRAM="/usr/bin/make.exe" \
    -DCMAKE_C_COMPILER="${MINGW_PREFIX}/bin/gcc.exe" \
    -DCMAKE_CXX_COMPILER="${MINGW_PREFIX}/bin/g++.exe" \
    -DFFI_INCLUDE_DIR="${FFI_INCLUDE_DIR}" \
    -DCMAKE_C_FLAGS="${CFLAGS}" \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS} ${CPPFLAGS}" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DLLVM_TARGETS_TO_BUILD="ARM;CppBackend;X86" \
    -DLLVM_ENABLE_ASSERTIONS=OFF \
    -DLLVM_ENABLE_THREADS=1 \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python2.exe \
    -DLLVM_ENABLE_FFI=ON \
    -DLLVM_ENABLE_SPHINX=ON \
    -DCMAKE_CXX_FLAGS="-D_GNU_SOURCE -D_LIBCPP_HAS_NO_CONSTEXPR" \
    -DLIBCLANG_BUILD_STATIC=ON \
    -DLIBCXX_ENABLE_SHARED=OFF \
    -DLIBCXXABI_ENABLE_SHARED=OFF \
    "${extra_config[@]}" \
    ../llvm

  make ${VERBOSE}

  # Disable automatic installation of components that go into subpackages
  # -i.orig to check what has been removed in-case it starts dropping more than it should
  #
  sed -i.orig '/\(clang\|lld\|lldb\)\/cmake_install.cmake/d' tools/cmake_install.cmake
  sed -i.orig '/\(extra\|scan-build\|scan-view\)\/cmake_install.cmake/d' tools/clang/tools/cmake_install.cmake
  sed -i.orig '/\(compiler-rt\|libcxxabi\|libcxx\)\/cmake_install.cmake/d' projects/cmake_install.cmake
}

#check() {
#  cd "${srcdir}"/build-${CARCH}
#  # Remove || true once testcase doesn't cause failures.
#  make check || true
#}

package_llvm-svn() {
  pkgdesc="Low Level Virtual Machine (svn) (mingw-w64)"
  #depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs") # "compiler-rt"
  provides=("${MINGW_PACKAGE_PREFIX}-llvm")
  conflicts=("${MINGW_PACKAGE_PREFIX}-llvm")
  install=llvm-${CARCH}.install

  pkgver=$(pkgver_internal "${srcdir}"/llvm)

  cd "${srcdir}"/llvm

  make -C ../build-${CARCH} DESTDIR="${pkgdir}" install

  install -Dm644 LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/llvm/LICENSE"

  # Install CMake stuff
  install -d "${pkgdir}"${MINGW_PREFIX}/share/llvm/cmake/{modules,platforms}
  install -Dm644 "${srcdir}"/llvm/cmake/modules/*.cmake "${pkgdir}"${MINGW_PREFIX}/share/llvm/cmake/modules/
  install -Dm644 "${srcdir}"/llvm/cmake/platforms/*.cmake "${pkgdir}"${MINGW_PREFIX}/share/llvm/cmake/platforms/

  # FileCheck is needed to build rust.
  install -Dm755 "${srcdir}"/build-${CARCH}/bin/FileCheck.exe "${pkgdir}${MINGW_PREFIX}/bin/FileCheck.exe"
}

package_compiler-rt-svn() {
  pkgdesc="Runtime libraries for Clang and LLVM (svn) (mingw-w64)"
  url="http://compiler-rt.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm-svn")
  provides=("${MINGW_PACKAGE_PREFIX}-compiler-rt")

  conflicts=("${MINGW_PACKAGE_PREFIX}-compiler-rt")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/projects/compiler-rt)

  cd "${srcdir}/llvm"
  make -C ../build-${CARCH}/projects/compiler-rt DESTDIR="${pkgdir}" install
 }

package_libcxxabi-svn() {
  pkgdesc="C++ Standard Library Support (svn) (mingw-w64)"
  url="http://libcxxabi.llvm.org/"
  provides=("${MINGW_PACKAGE_PREFIX}-libc++abi")
  conflicts=("${MINGW_PACKAGE_PREFIX}-libc++abi")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/projects/libcxxabi)

  cd "${srcdir}/llvm"
  make -C ../build-${CARCH}/projects/libcxxabi -j1 DESTDIR="${pkgdir}" install
}

package_libcxx-svn() {
  pkgdesc="C++ Standard Library (svn) (mingw-w64)"
  url="http://libcxx.llvm.org/"
  provides=("${MINGW_PACKAGE_PREFIX}-libc++")
  conflicts=("${MINGW_PACKAGE_PREFIX}-libc++")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/projects/libcxx)

  cd "${srcdir}/llvm"
  make -C ../build-${CARCH}/projects/libcxx -j1 DESTDIR="${pkgdir}" install
}

package_lldb-svn() {
  pkgdesc="Next generation, high-performance debugger (svn) (mingw-w64)"
  url="http://lldb.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-readline"
           "${MINGW_PACKAGE_PREFIX}-libxml2"
           "${MINGW_PACKAGE_PREFIX}-python2")

  cd "${srcdir}/llvm"

  make -C ../build-${CARCH}/tools/lldb DESTDIR="${pkgdir}" install

  # Compile Python scripts
  python2 -m compileall "${pkgdir}${MINGW_PREFIX}/lib/python2.7/site-packages/lldb"
  python2 -O -m compileall "${pkgdir}${MINGW_PREFIX}/lib/python2.7/site-packages/lldb"

  install -Dm644 tools/lldb/LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/$pkgname/LICENSE"
}

package_lld-svn() {
  pkgdesc="Linker tools for LLVM (svn) (mingw-w64)"
  url="http://lld.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm-svn")
  provides=("${MINGW_PACKAGE_PREFIX}-lld")
  conflicts=("${MINGW_PACKAGE_PREFIX}-lld")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/tools/lld)

  cd "${srcdir}/llvm"
  make -C ../build-${CARCH}/tools/lld -j1 DESTDIR="${pkgdir}" install
}

package_clang-svn() {
  pkgdesc="C language family frontend for LLVM (svn) (mingw-w64)"
  url="http://clang.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm-svn")
  provides=("${MINGW_PACKAGE_PREFIX}-clang")
  conflicts=("${MINGW_PACKAGE_PREFIX}-clang")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/tools/clang)

  cd "${srcdir}/llvm"
  make -C ../build-${CARCH}/tools/clang DESTDIR="${pkgdir}" install

  # Install static clang library ..
  cp ../build-${CARCH}/lib/libclang.a ${pkgdir}${MINGW_PREFIX}/lib/
}

package_clang-analyzer-svn() {
  pkgdesc="A source code analysis framework (svn) (mingw-w64)"
  url="http://clang-analyzer.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-clang-svn"
           "${MINGW_PACKAGE_PREFIX}-python2")
  provides=("${MINGW_PACKAGE_PREFIX}-clang-analyzer")
  conflicts=("${MINGW_PACKAGE_PREFIX}-clang-analyzer")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/tools/clang)

  cd "${srcdir}"/llvm
  make -C ../build-${CARCH}/tools/clang/tools/scan-build -j1 DESTDIR="${pkgdir}" install
  make -C ../build-${CARCH}/tools/clang/tools/scan-view -j1 DESTDIR="${pkgdir}" install

  # Use Python 2
  sed -i \
    -e 's|env python$|&2|' \
    -e 's|/usr/bin/python$|&2|' \
    "${pkgdir}"${MINGW_PREFIX}/bin/scan-view

  # Compile Python scripts
  python2 -m compileall "${pkgdir}"${MINGW_PREFIX}/bin/clang-analyzer
  python2 -O -m compileall "${pkgdir}"${MINGW_PREFIX}/lib/clang-analyzer
  install -Dm644 LICENSE.TXT "${pkgdir}"${MINGW_PREFIX}/share/licenses/clang-analyzer/LICENSE
}

package_clang-tools-extra-svn() {
  pkgdesc="Extra tools built using Clang's tooling APIs (svn) (mingw-w64)"
  url="http://clang.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-clang-svn")
  provides=("${MINGW_PACKAGE_PREFIX}-clang-tools-extra")
  conflicts=("${MINGW_PACKAGE_PREFIX}-clang-tools-extra")
  pkgver=$(pkgver_internal "${srcdir}"/llvm/tools/clang/tools/extra)

  cd "${srcdir}"/llvm
  make -C ../build-${CARCH}/tools/clang/tools/extra -j1 DESTDIR="${pkgdir}" install
}

# Wrappers
package_mingw-w64-i686-clang-svn(){
  package_clang-svn
}

package_mingw-w64-i686-clang-analyzer-svn(){
  package_clang-analyzer-svn
}

package_mingw-w64-i686-clang-tools-extra-svn(){
  package_clang-tools-extra-svn
}

package_mingw-w64-i686-compiler-rt-svn(){
  package_compiler-rt-svn
}

package_mingw-w64-i686-libc++abi-svn(){
  package_libcxxabi-svn
}

package_mingw-w64-i686-libc++-svn(){
  package_libcxx-svn
}

package_mingw-w64-i686-lld-svn(){
  package_lld-svn
}

package_mingw-w64-i686-lldb-svn(){
  package_lldb-svn
}

package_mingw-w64-i686-llvm-svn(){
  package_llvm-svn
}

package_mingw-w64-x86_64-clang-svn(){
  package_clang-svn
}

package_mingw-w64-x86_64-clang-analyzer-svn(){
  package_clang-analyzer-svn
}

package_mingw-w64-x86_64-clang-tools-extra-svn(){
  package_clang-tools-extra-svn
}

package_mingw-w64-x86_64-compiler-rt-svn(){
  package_compiler-rt-svn
}

package_mingw-w64-x86_64-libc++abi-svn(){
  package_libcxxabi-svn
}

package_mingw-w64-x86_64-libc++-svn(){
  package_libcxx-svn
}

package_mingw-w64-x86_64-lld-svn(){
  package_lld-svn
}

package_mingw-w64-x86_64-lldb-svn(){
  package_lldb-svn
}

package_mingw-w64-x86_64-llvm-svn(){
  package_llvm-svn
}

# vim:set ts=2 sw=2 et:
